package com.yz.crm.phase.service.impl;

import com.yz.common.model.Pager;
import com.yz.common.util.IDUtils;
import com.yz.crm.db.dao.BasePhaseItemMapper;
import com.yz.crm.db.dao.BasePhaseMapper;
import com.yz.crm.db.model.BasePhase;
import com.yz.crm.db.model.BasePhaseExample;
import com.yz.crm.db.model.BasePhaseItem;
import com.yz.crm.db.model.BasePhaseItemExample;
import com.yz.crm.phase.constant.PhaseConstant;
import com.yz.crm.phase.service.PhaseService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by DOVE on 2017/5/18 0018.
 */
@Service("phaseService")
public class PhaseServiceImpl implements PhaseService {
    private static final Log log = LogFactory.getLog(PhaseServiceImpl.class);
    @Resource
    private SqlSession sqlSession;

    private BasePhaseMapper getPhaseMapper() {
        return this.sqlSession.getMapper(BasePhaseMapper.class);
    }

    private BasePhaseItemMapper getPhaseItemMapper() {
        return this.sqlSession.getMapper(BasePhaseItemMapper.class);
    }


    @Override
    public Pager findPager(Pager pager) {
        int pageNumber = pager.getPageNumber();
        int pageSize = pager.getPageSize();
        int startIndex = (pageNumber - 1) * pageSize;
        if (pager.getStartIndex() > 0) {
            startIndex = (int) (startIndex + pager.getStartIndex());
        }
        BasePhaseMapper phaseMapper = this.getPhaseMapper();
        BasePhaseExample example = new BasePhaseExample();
        BasePhaseExample.Criteria criteria = example.createCriteria();
        Map keys = pager.getKeys();
        //根据公司获取公司相关阶段
        if (keys.containsKey(PhaseConstant.PAGER_KEY_COMPANY_ID)) {
            String companyId = Objects.toString(pager.getMapping(PhaseConstant.PAGER_KEY_COMPANY_ID), "");
            if (StringUtils.isNotEmpty(companyId)) {
                criteria.andCompanyIdEqualTo(companyId);
            }
        }
        //计算总数
        int totalCount = phaseMapper.countByExample(example);
        //分页数据
        example.setLimitStart(startIndex);
        example.setLimitEnd(pageSize);
        example.setOrderByClause("modify_date desc");
        List<BasePhase> list = phaseMapper.selectByExample(example);
        if (list == null) {
            list = new ArrayList<BasePhase>();
        }
        pager.setTotalCount(totalCount);
        pager.setResult(list);
        //计算页数
        pager.calculatePage();
        return pager;
    }

    @Override
    public int add(BasePhase phase) {
        BasePhaseMapper mapper = this.getPhaseMapper();
        Date now = new Date();
        if (StringUtils.isEmpty(phase.getId())) {
            phase.setId(IDUtils.getObjectId());
            phase.setCreateDate(now);
            phase.setModifyDate(now);
            return mapper.insert(phase);
        }
        return 0;
    }

    @Override
    public BasePhase getById(String id) {
        BasePhaseMapper mapper = this.getPhaseMapper();
        BasePhaseExample example = new BasePhaseExample();
        example.createCriteria().andIdEqualTo(id);
        List<BasePhase> list = mapper.selectByExample(example);
        if (list == null) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public List<BasePhaseItem> findListByPhaseId(String phaseId) {
        BasePhaseItemMapper mapper = this.getPhaseItemMapper();
        BasePhaseItemExample example = new BasePhaseItemExample();
        example.createCriteria().andPhaseIdEqualTo(phaseId);
        example.setOrderByClause(" order_num desc");
        List<BasePhaseItem> list = mapper.selectByExample(example);
        return list;
    }

    @Override
    public int addPhaseItem(List<BasePhaseItem> itemList) {
        int index = 0;
        int sum = itemList.size();
        for (BasePhaseItem item : itemList) {
            if (item.getOrderNum() == null) {
                item.setOrderNum(sum - index);
            }
            saveOrUpdate(item);
            index++;
        }
        return 0;
    }

    public int saveOrUpdate(BasePhaseItem phaseItem) {
        if (StringUtils.isEmpty(phaseItem.getId())) {
            phaseItem.setId(IDUtils.getObjectId());
            return getPhaseItemMapper().insert(phaseItem);
        } else {
            return getPhaseItemMapper().updateByPrimaryKey(phaseItem);
        }
    }
}
